package zhang.lao.api.service;/**
 * Created by zhangqin on 2017/11/23.
 */

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhang.lao.build.mybatis.jdbc.auto.model.*;
import zhang.lao.build.redis.RedisUtil;
import zhang.lao.build.tool.CoordinateConversion;
import zhang.lao.build.tool.GCJ2WGSUtils;
import zhang.lao.build.tool.ListUtils;
import zhang.lao.build.tool.date.DateStyle;
import zhang.lao.build.tool.date.DateUtil;
import zhang.lao.dao.base.*;
import zhang.lao.extents.spring.exception.ApiException;
import zhang.lao.pojo.api.req.*;
import zhang.lao.pojo.api.resp.ApiDeviceWorkReportResp;
import zhang.lao.pojo.api.resp.ApiRespData;
import zhang.lao.pojo.api.resp.ApiResultEnum;
import zhang.lao.pojo.console.map.MapSerieResp;
import zhang.lao.pojo.console.report.AreaReportResp;
import zhang.lao.pojo.console.resp.HttpResultUtil;
import zhang.lao.service.console.ReportService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @create 2017-11-23 17:58
 **/

@Service
public class ApiReportService {
    @Autowired
    private ReportService reportService;
    @Autowired
    private WnMachineDeviceDao wnMachineDeviceDao;
    @Autowired
    private WnMachineWorkReportGt06Dao wnMachineWorkReportGt06Dao;


    @Autowired
    private WnLnglatGt06NewDao wnLnglatGt06NewDao;
    @Autowired
    private WnLnglatAutomaticNewDao wnLnglatAutomaticNewDao;

    public ApiRespData areaReport(ApiReqData<ApiAreaReportReq> req){
        ApiAreaReportReq apiAreaReportReq = req.getBody();
        List<String> farms = Lists.newArrayList(apiAreaReportReq.getFarmIds().split(","));
        String startDate = apiAreaReportReq.getStartDate();
        String endDate = apiAreaReportReq.getEndDate();
        return ApiRespData.buildSucc(reportService.getAreaReportRespGt06(startDate,endDate,farms));
    }

    public ApiRespData activeReport(ApiReqData<ApiActiveReportReq> req){
        ApiActiveReportReq apiAreaReportReq = req.getBody();
        List<String> farms = Lists.newArrayList(apiAreaReportReq.getFarmIds().split(","));
        String startDate = apiAreaReportReq.getStartDate();
        String endDate = apiAreaReportReq.getEndDate();
        return ApiRespData.buildSucc(reportService.getActiveRespGt06(startDate,endDate,farms));
    }

    public ApiRespData resourceReport(ApiReqData<ApiResourceReportReq> req){
        ApiResourceReportReq apiAreaReportReq = req.getBody();
        List<String> farms = Lists.newArrayList(apiAreaReportReq.getFarmIds().split(","));
        return ApiRespData.buildSucc(reportService.resourceReport(farms));
    }
    public ApiRespData deviceWorkReport(ApiReqData<ApiDeviceWorkReportReq> req){
        ApiDeviceWorkReportReq apiDeviceWorkReportReq = req.getBody();
        Integer deviceId ;
        WnMachineDeviceExample wnMachineDeviceExample = new WnMachineDeviceExample();
        wnMachineDeviceExample.createCriteria().andDeviceNumberEqualTo(apiDeviceWorkReportReq.getDeviceNumber());
        WnMachineDevice wnMachineDevice = ListUtils.getFirst(wnMachineDeviceDao.selectByExample(wnMachineDeviceExample));
        if (wnMachineDevice == null) {
           throw new ApiException(ApiResultEnum.NODEVICE);
        }
        deviceId = wnMachineDevice.getId();
        WnMachineWorkReportGt06Example wnMachineWorkReportGt06Example = new WnMachineWorkReportGt06Example();
        wnMachineWorkReportGt06Example.createCriteria().andDeviceIdEqualTo(deviceId).andWorkDateEqualTo(apiDeviceWorkReportReq.getWorkDate());
        List<WnMachineWorkReportGt06> wnMachineWorkReportGt06s = wnMachineWorkReportGt06Dao.selectByExample(wnMachineWorkReportGt06Example);
       if(ListUtils.isNotEmpty(wnMachineWorkReportGt06s)){
           List<ApiDeviceWorkReportResp> apiDeviceWorkReportRespList = Lists.newArrayList();
           for (WnMachineWorkReportGt06 wnMachineWorkReportGt06 : wnMachineWorkReportGt06s) {
               ApiDeviceWorkReportResp apiDeviceWorkReportResp = new ApiDeviceWorkReportResp();
               apiDeviceWorkReportResp.setArea(wnMachineWorkReportGt06.getWorkArea().toString());
               apiDeviceWorkReportResp.setBreadth(wnMachineWorkReportGt06.getWorkBreadth().toString());
               apiDeviceWorkReportResp.setStartTime(DateUtil.DateToString(wnMachineWorkReportGt06.getStartTime(),DateStyle.YYYY_MM_DD_HH_MM_SS));
               apiDeviceWorkReportResp.setEndTime(DateUtil.DateToString(wnMachineWorkReportGt06.getEndTime(),DateStyle.YYYY_MM_DD_HH_MM_SS));
               apiDeviceWorkReportResp.setFarmId(wnMachineWorkReportGt06.getFarmId());
             //distance_all * ;
               double v = wnMachineWorkReportGt06.getWorkBreadth().doubleValue() * 1000 * 0.0015;
               BigDecimal divide = new BigDecimal(wnMachineWorkReportGt06.getWorkArea()).divide(new BigDecimal(v),10,BigDecimal.ROUND_HALF_UP);
               divide=divide.setScale(2,BigDecimal.ROUND_HALF_UP);
               apiDeviceWorkReportResp.setLength(divide.toEngineeringString());
               apiDeviceWorkReportRespList.add(apiDeviceWorkReportResp);
           }
           return ApiRespData.buildSucc(apiDeviceWorkReportRespList);
       }

        return ApiRespData.buildFail(ApiResultEnum.NODATE);
    }

    public ApiRespData<JSONObject> getNewLocation(int deviceId, int type){
        if(type==1){
            WnLnglatGt06NewExample wnLnglatGt06NewExample = new WnLnglatGt06NewExample();
            wnLnglatGt06NewExample.createCriteria().andDeviceIdEqualTo(deviceId);
            wnLnglatGt06NewExample.setOrderByClause("create_time desc");
            PageHelper.startPage(1,1);
            WnLnglatGt06New first = ListUtils.getFirst(wnLnglatGt06NewDao.selectByExample(wnLnglatGt06NewExample));
            if(first==null){
                return ApiRespData.buildFail(ApiResultEnum.NODATE);
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("lng", first.getLng());
            jsonObject.put("lat", first.getLat());
            jsonObject.put("degree", first.getOrentation());
            jsonObject.put("speed",first.getSpeed());
            jsonObject.put("work",(new Date().getTime()-first.getCreateTime().getTime())<15000);
            jsonObject.put("time",DateUtil.DateToString(first.getCreateTime(),DateStyle.YYYY_MM_DD_HH_MM_SS));
            return ApiRespData.buildSucc(jsonObject);
        }else{
            WnLnglatAutomaticNewExample wnLnglatGt06NewExample = new WnLnglatAutomaticNewExample();
            wnLnglatGt06NewExample.createCriteria().andDeviceIdEqualTo(deviceId);
            wnLnglatGt06NewExample.setOrderByClause("create_time desc");
            PageHelper.startPage(1,1);
            WnLnglatAutomaticNew first = ListUtils.getFirst(wnLnglatAutomaticNewDao.selectByExample(wnLnglatGt06NewExample));
            if(first==null){
                return ApiRespData.buildFail(ApiResultEnum.NODATE);
            }

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("lng", GCJ2WGSUtils.WGSLon(Double.parseDouble(first.getLat()), Double.valueOf(first.getLng())));
            jsonObject.put("lat", GCJ2WGSUtils.WGSLat(Double.parseDouble(first.getLat()), Double.valueOf(first.getLng())));
            jsonObject.put("degree", first.getOrentation());
            jsonObject.put("speed",first.getSpeed());
            jsonObject.put("work",(new Date().getTime()-first.getCreateTime().getTime())<15000);
            jsonObject.put("time",DateUtil.DateToString(first.getCreateTime(),DateStyle.YYYY_MM_DD_HH_MM_SS));


            return ApiRespData.buildSucc(jsonObject);
        }

    }
}
